package com.zhiche.lisa.bms.controller.arfund;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.arfund.ArFund;
import com.zhiche.lisa.bms.pojo.vo.arFund.ArFundVO;
import com.zhiche.lisa.bms.service.arfund.ArFundService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 其他扣款 前端控制器
 * </p>
 *
 * @author hongbo
 * @since 2018-09-20
 */
@RestController
@RequestMapping(value = "/arFund")
@Api(value = "feePayable",description = "其他扣款",tags = "其他扣款")
public class ArFundController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ArFundController.class);
    @Autowired
    private ArFundService arFundService;

    /**
     * 其他扣款分页查询
     *
     * @param pageVo
     * @return
     */
    @PostMapping("/queryArFundLine")
    @ApiOperation(value = "其他扣款分页查询", notes = "其他扣款分页查询", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Page<ArFund>> queryArFundLine(@RequestBody Page<ArFundVO> pageVo) {
        LOGGER.info("ArFundController.queryArFundLine param: {}", pageVo);
        RestfulResponse<Page<ArFund>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            Page<ArFund> page = arFundService.queryArFundLinePage(pageVo);
            result.setData(page);
        } catch (BaseException e) {
            LOGGER.error("ArFundController.queryArFundLine error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ArFundController.queryArFundLine error:{}", e.getMessage());
            result.setMessage("其他扣款分页查询异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @GetMapping(value = "/getArFundLineByPayIdList")
    @ApiOperation(value = "根据支付申请Id，查询其他扣款列表", notes = "根据支付申请Id，查询其他扣款列表", httpMethod = "GET")
    public RestfulResponse<List<ArFund>> getArFundLineByPayIdList(@RequestParam Long payId) {
        LOGGER.info("ArFundController.getArFundLineByPayIdList param: {}", payId);
        RestfulResponse<List<ArFund>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            List<ArFund> page = arFundService.getArFundLineByPayIdList(payId);
            result.setData(page);
        } catch (BaseException e) {
            LOGGER.error("ArFundController.getArFundLineByPayIdList error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ArFundController.getArFundLineByPayIdList error:{}", e.getMessage());
            result.setMessage("其他扣款查询异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 其他扣款批量删除
     *
     * @param ids
     */
    @PostMapping(value = "/deleteArFund")
    @ApiOperation(value = "其他扣款批量删除", notes = "其他扣款批量删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<String> deleteArFund(@RequestBody List<Long> ids) {
        LOGGER.info("ArFundController.deleteArFund param: {}", ids);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            arFundService.deleteArFund(ids);
        } catch (BaseException e) {
            LOGGER.error("ArFundController.deleteArFund error: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ArFundController.deleteArFund error: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 其他扣款审核
     *
     * @param ids
     */
    @PostMapping(value = "/examineArFund")
    @ApiOperation(value = "其他扣款审核", notes = "其他扣款审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<String>> examineArFund(@RequestBody List<Long> ids, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("ArFundController.examineArFund param : {}", ids);
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "审核成功", null);
        try {
            List<String> errorList = arFundService.examineArFund(ids, authorization);
            if (!CollectionUtils.isEmpty(errorList)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "异常信息", null);
                result.setData(errorList);
            }
        } catch (BaseException e) {
            LOGGER.error("ArFundController.examineArFund error: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ArFundController.examineArFund error: {}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}

